/**
 * jims
 */
package com.jims.api.v1.controller.doctor;


import com.jims.master.common.data.Message;
import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.common.utils.SendSms;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.base.netHosptial.entity.NetSetting;
import com.jims.master.modules.base.netHosptial.service.NetSettingService;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.entity.MasterDoctorinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.base.user.service.MasterDoctorinfoService;
import com.jims.master.modules.sys.entity.HospitalDict;
import com.jims.master.modules.sys.entity.OrgDict;
import com.jims.master.modules.sys.service.HospitalDictService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller
 * @author WangYQ
 * @version 2017-05-23
 */
@Controller
@RequestMapping(value = "/netSetting")
public class NetSettingController extends BaseController {

	@Autowired
	private NetSettingService netSettingService;

    @Autowired
    private MasterBaseinfoService masterBaseinfoService;

    @Autowired
    private HospitalDictService hospitalDictService;
    @Autowired
	private MasterDoctorinfoService masterDoctorinfoService;

	@ModelAttribute
	public NetSetting get(@RequestParam(required=false) String id) {
		NetSetting entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = netSettingService.get(id);
		}
		if (entity == null){
			entity = new NetSetting();
		}
		return entity;
	}
	/**
	 * @Title: contractHospitalV100
	 * @Description: (申请签约医院)
	 * @param doctorId 医生id
	 * @param orgId 机构id
	 * @return
	 * @author Wang	YQ
	 * @date 2018/03/30
	 * @throws
	 */
	@RequestMapping(value = "V100/contractHospital",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String contractHospital(@RequestParam(required = true)String doctorId,@RequestParam(required = true)String orgId) {
		Message msg=new Message();
		MasterDoctorinfo masterDoctorinfo=masterDoctorinfoService.get(doctorId);
		NetSetting netSetting=new NetSetting();
		netSetting.setApplyFlag("1");
		HospitalDict hospitalDict=new HospitalDict();
		hospitalDict.setId(orgId);
		netSetting.setHosiptial(hospitalDict);
//		判断查找的对象是不是空
		if(masterDoctorinfo!=null){
			if(masterDoctorinfo.getBaseinfo()!=null){
				netSetting.setDoctorBaseinfo(masterDoctorinfo.getBaseinfo().getId());
			}
		}
		int applied=netSettingService.judgeAppliedHospital(netSetting);
		if(applied>0){
			msg.setStatus("1");
			msg.setData("您已申请签约该医院");
		}else{
			int i=netSettingService.save(netSetting);
			if(i>0){
//			保存成功后获取已申请医院
				List<OrgDict> list=netSettingService.findAppliedHospital(doctorId);
				if(list!=null&&list.size()>0){
					msg.setStatus("0");
					msg.setData(list);
				}else {
					msg.setStatus("0");
					msg.setData("暂无申请签约医院");
				}
			}else{
				msg.setStatus("1");
				msg.setData("申请失败");
			}
		}
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * @Title: judgeAppledHospitalV100
	 * @Description: (判断是否签约该医院)
	 * @param doctorId 医生id
	 * @param orgId 机构id
	 * @return
	 * @author Wang	YQ
	 * @date 2018/03/30
	 * @throws
	 */
	@RequestMapping(value = "V100/judgeAppliedHospital",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String judgeAppliedHospital(@RequestParam(required = true)String doctorId,@RequestParam(required = true)String orgId) {
		Message msg=new Message();
		MasterDoctorinfo masterDoctorinfo=masterDoctorinfoService.get(doctorId);
		NetSetting netSetting=new NetSetting();
		HospitalDict hospitalDict=new HospitalDict();
		hospitalDict.setId(orgId);
		netSetting.setHosiptial(hospitalDict);
//		判断查找的对象是不是空
		if(masterDoctorinfo!=null){
			if(masterDoctorinfo.getBaseinfo()!=null){
				netSetting.setDoctorBaseinfo(masterDoctorinfo.getBaseinfo().getId());
			}
		}
		int i=netSettingService.judgeAppliedHospital(netSetting);
			if(i>0){
			msg.setStatus("0");
			msg.setData("已申请该医院");
		}else{
			msg.setStatus("1");
			msg.setData("可以申请该医院");
		}
		return SecurityUtil.encryptMsgData(msg);
	}
	/**
	 * @Title: findAppliedHospitalV100
	 * @Description: (查询已申请签约医院)
	 * @param doctorId 医生id
	 * @return
	 * @author Wang	YQ
	 * @date 2018/03/30
	 * @throws
	 */
	@RequestMapping(value = "V100/findAppliedHospital",method ={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String findAppliedHospital(@RequestParam(required = true)String doctorId){
		Message msg=new Message();
		List<OrgDict> list=netSettingService.findAppliedHospital(doctorId);
		if(list!=null&&list.size()>0){
			msg.setStatus("0");
			msg.setData(list);
		}else{
			msg.setStatus("1");
			msg.setData("赞无签约医院");
		}
		return SecurityUtil.encryptMsgData(msg);
	}
}